
package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iss.view.common.ToastAlone;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.R;
import com.v1.video.util.Logger;
import com.v1.video.view.SlideSwitch;
import com.v1.video.view.SlideSwitch.OnSwitchChangedListener;
import com.v1.video.widgets.MyDialog;

import java.io.File;

/**
 * 系统设置类
 * 
 * @author Administrator
 * update lixinli 2014/6/19
 *  个人信息和关于第一视频增加友盟点击事件统计
 */
public class PersonalOptionActivity extends BaseActivity implements OnSwitchChangedListener {

    public final String TAG = "PersonalOptionActivity";

    private PersonalOptionActivity instance;

//    RelativeLayout layout_personal_info = null;// 个人信息

//    RelativeLayout layout_acount_bind = null;// 账号绑定

    RelativeLayout layout_detail_auto = null;// 详情页自动播放

    RelativeLayout layout_upload_set = null;// 上传设置

//    RelativeLayout layout_users_experence = null;// 用户体验

//    RelativeLayout layout_news_warn = null;// 消息提醒

    RelativeLayout layout_clean_cache = null;// 清理缓存

    RelativeLayout layout_about_v1 = null;// 关于第一视频

    public static final int RESULT_UPLOAD_INTERNET = 0;

    public static final int RESULT_USER_NICKNAME = 1;       

    /** 标题 */
    TextView text_title;

    /** 返回按钮 */
    private View  view_lay_result;

    /** 网络上传的方式 */
    private TextView txt_net_select;

    /** 详情自动播放按钮 */
    private ImageView view_btn_auto;
//    private SlideSwitch view_btn_auto;
//    ToggleButton view_btn_auto;
    /** 是否开启自动播放，默认为关 */
    private boolean isAutoOpen = true;
    private TextView txtMemory = null;
    public static boolean isClearMemory = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_option);
        instance = this;
    }    

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
//        layout_personal_info = (RelativeLayout) findViewById(R.id.option_personal_info_back);
//        layout_acount_bind = (RelativeLayout) findViewById(R.id.option_acount_bind_back);
        layout_detail_auto = (RelativeLayout) findViewById(R.id.option_frame_details);
        layout_upload_set = (RelativeLayout) findViewById(R.id.option_frame_upload_bg);
//        layout_users_experence = (RelativeLayout) findViewById(R.id.option_frame_users_experience);
//        layout_news_warn = (RelativeLayout) findViewById(R.id.option_frame_news_warn);
        layout_clean_cache = (RelativeLayout) findViewById(R.id.option_frame_clear_cache);
        layout_about_v1 = (RelativeLayout) findViewById(R.id.option_frame_about);
        view_lay_result =  findViewById(R.id.lay_result);
        text_title = (TextView) findViewById(R.id.tv_title);

        txt_net_select = (TextView) findViewById(R.id.option_net_set);
//        view_btn_auto = (ImageView) findViewById(R.id.option_detail_btn);
        txtMemory = (TextView) findViewById(R.id.option_clear_cache_memory);
//        view_btn_auto = (SlideSwitch) findViewById(R.id.option_detail_btn);
//        view_btn_auto = (ToggleButton) findViewById(R.id.option_detail_btn);
    }

    @Override
    protected void initData() {
        // TODO Auto-generated method stub
        text_title.setText(R.string.option_bar_title);        
//        if(OptionInfo.getInstance(PersonalOptionActivity.this).isVideoDetailPageAutoPlay()){
//            isAutoOpen = true;
//            view_btn_auto.setImageResource(R.drawable.switch_on);
//        }else{
//            isAutoOpen = true;
//            view_btn_auto.setImageResource(R.drawable.switch_off); 
//        }
        getMemorySize();
    }
    
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd("设置页");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart("设置页");
	}

	/**
	 * 获取缓存大小
	 */
	private void getMemorySize() {
		long fileSize = 0;
		String cacheSize = "0KB";
		File filesPicDir = new File(
				android.os.Environment.getExternalStorageDirectory(),
				"Android/data/com.v1.video/cache");
		File cacheDir = getCacheDir();
		fileSize += getDirSize(filesPicDir);
		fileSize += getDirSize(cacheDir);
		if (fileSize > 0) {
			cacheSize = formatFileSize(fileSize);
		}
		txtMemory.setText(cacheSize);
	}

    @Override
    protected void setListener() {
        // TODO Auto-generated method stub
        btnOnClickProcess();
        layoutOnClickProcess();        
    }

    private void btnOnClickProcess() {
        view_lay_result.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
       
    }
    
    private void layoutOnClickProcess(){
//        layout_personal_info.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//            	
//            	if(LoginInfo.getInstance().isLogin()){
//            		Intent intent = new Intent();
//            		intent.setClass(PersonalOptionActivity.this, PersonInfoActivity.class);
//            		startActivity(intent);
//            	}
//            	else{
//            		Intent login = new Intent();
//                    login.setClass(PersonalOptionActivity.this, SettingLoginActivity.class);
//                    login.putExtra("loginFlag", 4);
//                    startActivity(login);
//            	}
//            	MobclickAgent.onEvent(PersonalOptionActivity.this, "personalPageSetting_id");
//            }
//        });
//        layout_acount_bind.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {}
//        });
        layout_upload_set.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(PersonalOptionActivity.this, OptionUploadSetActivity.class);
                startActivityForResult(intent, RESULT_UPLOAD_INTERNET);
            }
        });
        layout_detail_auto.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(PersonalOptionActivity.this, OptionAutoSetActivity.class);
                startActivity(intent);
            }
        });
//        layout_users_experence.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                Intent intent = new Intent();
//                intent.setClass(PersonalOptionActivity.this, OptionUserExperenceActivity.class);
//                startActivity(intent);
//            }
//        });
//        layout_news_warn.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                if(!LoginInfo.getInstance().isLogin()){
//                    Intent login = new Intent();
//                    login.setClass(PersonalOptionActivity.this, SettingLoginActivity.class);
//                    login.putExtra("loginFlag", 4);
//                    startActivity(login);
//                    return;
//                }
//                Intent intent = new Intent();
//                intent.setClass(PersonalOptionActivity.this, OptionInformationTipsActivity.class);
//                startActivity(intent);
//            }
//        });
        layout_about_v1.setOnClickListener(new OnClickListener() {
        	
            @Override
            public void onClick(View arg0) {
                MobclickAgent.onEvent(PersonalOptionActivity.this, "aboutVod1_id");
                Intent intent = new Intent();
                intent.setClass(PersonalOptionActivity.this, AboutActivity.class);
                startActivity(intent);
            }
        });
        layout_clean_cache.setOnClickListener(new OnClickListener() {
        	
            @Override
            public void onClick(View arg0) {
                if(!isClearMemory){
                	showClearCacheLayout();
                }else{
                	
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_UPLOAD_INTERNET) {
            if (resultCode == OptionUploadSetActivity.PASS_NET_ANY) {
                txt_net_select.setText(R.string.upload_label_any_inter);
            } else {
                txt_net_select.setText(R.string.upload_label_only_wifi);
            }
            Logger.i(TAG, "onActivityResult调用了");
        } else if (requestCode == RESULT_USER_NICKNAME) {

        }
    }

    @Override
    public void onSwitchChanged(SlideSwitch obj, int status) {
        // TODO Auto-generated method stub
        
    }
    
    private void showClearCacheLayout() {
		final MyDialog dialogCommon = new MyDialog(this,
				R.style.dialog_headsetting);
		LinearLayout linearlayout;
		android.view.WindowManager.LayoutParams layoutparams;
		linearlayout = (LinearLayout) ((LayoutInflater) this
				.getSystemService("layout_inflater")).inflate(
				R.layout.dialog_memory_clear, null);
		linearlayout.setMinimumWidth(10000);

		layoutparams = dialogCommon.getWindow().getAttributes();
		layoutparams.x = 0;
		layoutparams.y = -1000;
		layoutparams.gravity = 80;
		dialogCommon.onWindowAttributesChanged(layoutparams);

		dialogCommon.setCanceledOnTouchOutside(true);
		dialogCommon.setContentView(linearlayout);
		
		dialogCommon.findViewById(R.id.ll_report).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialogCommon.dismiss();
						ImageLoader.getInstance().getDiscCache().clear();
						ImageLoader.getInstance().getMemoryCache().clear();
						ToastAlone.showToast(PersonalOptionActivity.this, "缓存已清理", 1);
						txtMemory.setText("0KB");
						PersonalOptionActivity.isClearMemory = true;
					}
				});
		dialogCommon.findViewById(R.id.ll_cancel).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialogCommon.dismiss();
					}
				});
		dialogCommon.show();
	}
    
    
    
    /**
	 * 转换文件大小
	 * 
	 * @param fileS
	 * @return B/KB/MB/GB
	 */
	private String formatFileSize(long fileS) {
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "KB";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "MB";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}

	/**
	 * 获取目录文件大小
	 * 
	 * @param dir
	 * @return
	 */
	private long getDirSize(File dir) {
		if (dir == null) {
			return 0;
		}
		if (!dir.isDirectory()) {
			return 0;
		}
		long dirSize = 0;
		File[] files = dir.listFiles();
		for (File file : files) {
			if (file.isFile()) {
				dirSize += file.length();
			} else if (file.isDirectory()) {
				dirSize += file.length();
				dirSize += getDirSize(file); // 递归调用继续统计
			}
		}
		return dirSize;
	}
}
